package org.kl.wxWeb.web;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.User;
import org.kl.bf.service.basic.UserService;
import org.kl.bf.utils.BasicConstants;
import org.kl.bf.web.base.BasicController;
import org.kl.wx.entity.WxUser;
import org.kl.wx.service.AppService;
import org.kl.wx.service.EventRecordService;
import org.kl.wx.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.security.utils.Digests;
import org.springside.modules.utils.Encodes;
import org.springside.modules.web.MediaTypes;

@Controller
@RequestMapping(value = "/wxWeb/bindingWxUser")
public class WxUseBindingrController extends BasicController {

	public static final int HASH_INTERATIONS = 1024;
	@Autowired
	private WxUserService wxUserService;
	@Autowired
	private AppService appService;
	@Autowired
	private UserService userservice;
	@Autowired
	private EventRecordService eventRecordService;

	@RequestMapping(method = RequestMethod.GET)
	public String list(@RequestParam(value = "openid", defaultValue = "") String openid,
			@RequestParam(value = "appid", defaultValue = "") String appid, @RequestParam(value = "state", defaultValue = "") String state,
			Model modle) {
		if (StringUtils.isBlank(openid) || StringUtils.isBlank(appid)) {
			return "error/403";
		}
		modle.addAttribute("appid", appid);
		modle.addAttribute("openid", openid);
		modle.addAttribute("state", state);
		return "/wxWeb/wxUserbinding";
	}

	@RequestMapping(value = "bindingUser", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String update(@RequestParam(value = "appid", defaultValue = "") String appid,
			@RequestParam(value = "openid", defaultValue = "") String openid,
			@RequestParam(value = "loginNo", defaultValue = "") String loginNo,
			@RequestParam(value = "password", defaultValue = "") String password,
			@RequestParam(value = "state", defaultValue = "") String state) {
		try {
			if (StringUtils.isBlank(appid) || StringUtils.isBlank(openid) || StringUtils.isBlank(loginNo) || StringUtils.isBlank(password)) {
				return ajaxMsg("信息不完整，请重新填写!", false, null);
			}
			if (appService.getApp(appid) == null || StringUtils.isBlank(appService.getApp(appid).getId())) {
				return ajaxMsg("绑定失败,请关闭界面重新操作!", false, null);
			}
			WxUser wxUser = wxUserService.getByAppIdAndOpenid(appid, openid);
			if (wxUser == null || StringUtils.isBlank(wxUser.getId())) {
				return ajaxMsg("绑定失败,请关闭界面重新操作!", false, null);
			}
			User user = userservice.getByLoginNo(loginNo);
			if (user == null || StringUtils.isBlank(user.getId())) {
				return ajaxMsg("该工号不存在!", false, null);
			} else {
				if (!StringUtils.equals(user.getPassword(),
						Encodes.encodeHex(Digests.sha1(password.getBytes(), Encodes.decodeHex(user.getSalt()), HASH_INTERATIONS)))) {
					return ajaxMsg("工号或者密码不正确!", false, null);
				} else {
					if (wxUserService.getByUnionid(user.getId()) != null) {
						return ajaxMsg("该平台账号已绑定微信用户，不能再次绑定!", false, null);
					} else {
						wxUser.setUnionid(user.getId());
						wxUserService.saveWxUser(wxUser);
						String url = BasicConstants.SYSTOM_URL + "/wxWeb/" + state + "?openid=" + openid + "&appid=" + appid;
						return ajaxMsg("绑定成功!正在跳转到请求界面，请稍后!", true, url);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxMsg("绑定失败", false, null);
		}
	}
}
